package pe.com.bbva.encriptacion;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.security.Provider;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

public class BlowFish {

    //private static String strkey ="Blowfish";
    private static Base64 base64 = new Base64();

     //encrypt using blowfish algorithm
    public static String encriptar(String Data, String strkey)throws Exception{
        SecretKeySpec key = new SecretKeySpec(strkey.getBytes("UTF8"), "Blowfish");
        Cipher cipher = Cipher.getInstance("Blowfish");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return base64.encodeToString(cipher.doFinal(Data.getBytes("UTF8")));
    }

    //decrypt using blow fish algorithm
    public static String desEncriptar(String encrypted,String strkey)throws Exception{
        byte[] encryptedData = (byte[]) base64.decode(encrypted);
         SecretKeySpec key = new SecretKeySpec(strkey.getBytes("UTF8"), "Blowfish");
         Cipher cipher = Cipher.getInstance("Blowfish");
         cipher.init(Cipher.DECRYPT_MODE, key);
         byte[] decrypted = cipher.doFinal(encryptedData);
         return new String(decrypted); 

    }

}
